home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1994 November: Tool Chest / Dev.CD Nov 94.toast / Tool Chest / Development Tools & Languages / • Other Platforms / PCCTS / h / err.h < prev    next >
Encoding:
C/C++ Source or Header  |  1994-09-14  |  14.0 KB  |  596 lines  |  [TEXT/MPS ]

  1. /*
  2.  * err.h
  3.  *
  4.  * Standard error handling mechanism
  5.  *
  6.  * SOFTWARE RIGHTS
  7.  *
  8.  * We reserve no LEGAL rights to the Purdue Compiler Construction Tool
  9.  * Set (PCCTS) -- PCCTS is in the public domain.  An individual or
  10.  * company may do whatever they wish with source code distributed with
  11.  * PCCTS or the code generated by PCCTS, including the incorporation of
  12.  * PCCTS, or its output, into commerical software.
  13.  * 
  14.  * We encourage users to develop software with PCCTS.  However, we do ask
  15.  * that credit is given to us for developing PCCTS.  By "credit",
  16.  * we mean that if you incorporate our source code into one of your
  17.  * programs (commercial product, research project, or otherwise) that you
  18.  * acknowledge this fact somewhere in the documentation, research report,
  19.  * etc...  If you like PCCTS and have developed a nice tool with the
  20.  * output, please mention that you developed it using PCCTS.  In
  21.  * addition, we ask that this header remain intact in our source code.
  22.  * As long as these guidelines are kept, we expect to continue enhancing
  23.  * this system and expect to make other tools available as they are
  24.  * completed.
  25.  *
  26.  * Has grown to hold all kinds of stuff (err.h is increasingly misnamed)
  27.  *
  28.  * ANTLR 1.23
  29.  * Terence Parr
  30.  * Parr Research Corporation
  31.  * with Purdue University and AHPCRC, University of Minnesota
  32.  * 1989-1994
  33.  */
  34.  
  35. #ifndef ERR_H
  36. #define ERR_H
  37.  
  38. #include "config.h"
  39.  
  40. #include <string.h>
  41. #ifdef __STDC__
  42. #include <stdarg.h>
  43. #else
  44. #include <varargs.h>
  45. #endif
  46.  
  47. #ifdef DUM
  48. /* Define usable bits per unsigned int word (used for set stuff) */
  49. #ifdef PC
  50. #define BSETWORDSIZE 16
  51. #define BSETLOGWORDSIZE    4
  52. #else
  53. #define    BSETWORDSIZE 32
  54. #define BSETLOGWORDSIZE 5
  55. #endif
  56. #endif
  57.  
  58. #define    BSETWORDSIZE 8
  59. #define BSETLOGWORDSIZE 3        /* SetWordType is 8bits */
  60.  
  61. #define    BSETMODWORD(x) ((x) & (BSETWORDSIZE-1))        /* x % BSETWORDSIZE */
  62. #define    BSETDIVWORD(x) ((x) >> BSETLOGWORDSIZE)        /* x / BSETWORDSIZE */
  63.  
  64. /* This is not put into the global pccts_parser structure because it is
  65.  * hidden and does not need to be saved during a "save state" operation
  66.  */
  67. /* maximum of 32 bits/unsigned int and must be 8 bits/byte */
  68. static SetWordType bitmask[] = {
  69.     0x00000001, 0x00000002, 0x00000004, 0x00000008,
  70.     0x00000010, 0x00000020, 0x00000040, 0x00000080
  71. };
  72.  
  73. void
  74. #ifdef __USE_PROTOS
  75. zzresynch(SetWordType *wd,SetWordType mask)
  76. #else
  77. zzresynch(wd,mask)
  78. SetWordType *wd, mask;
  79. #endif
  80. {
  81.     static int consumed = 1;
  82.  
  83.     /* if you enter here without having consumed a token from last resynch
  84.      * force a token consumption.
  85.      */
  86.     if ( !consumed ) {zzCONSUME; return;}
  87.  
  88.     /* if current token is in resynch set, we've got what we wanted */
  89.     if ( wd[LA(1)]&mask || LA(1) == zzEOF_TOKEN ) {consumed=0; return;}
  90.     
  91.     /* scan until we find something in the resynch set */
  92.     while ( !(wd[LA(1)]&mask) && LA(1) != zzEOF_TOKEN ) {zzCONSUME;}
  93.     consumed=1;
  94. }
  95.  
  96. /* input looks like:
  97.  *        zzFAIL(k, e1, e2, ...,&zzMissSet,&zzMissText,&zzBadTok,&zzBadText)
  98.  * where the zzMiss stuff is set here to the token that did not match
  99.  * (and which set wasn't it a member of).
  100.  */
  101. void
  102. #ifdef __USE_PROTOS
  103. zzFAIL(int k, ...)
  104. #else
  105. zzFAIL(va_alist)
  106. va_dcl
  107. #endif
  108. {
  109. #ifdef LL_K
  110.     static char text[LL_K*ZZLEXBUFSIZE+1];
  111.     SetWordType *f[LL_K];
  112. #else
  113.     static char text[ZZLEXBUFSIZE+1];
  114.     SetWordType *f[1];
  115. #endif
  116.     SetWordType **miss_set;
  117.     char **miss_text;
  118.     int *bad_tok;
  119.     char **bad_text;
  120.     int *err_k;
  121.     int i;
  122.     va_list ap;
  123. #ifndef __USE_PROTOS
  124.     int k;
  125. #endif
  126. #ifdef __USE_PROTOS
  127.     va_start(ap, k);
  128. #else
  129.     va_start(ap);
  130.     k = va_arg(ap, int);    /* how many lookahead sets? */
  131. #endif
  132.     text[0] = '\0';
  133.     for (i=1; i<=k; i++)    /* collect all lookahead sets */
  134.     {
  135.         f[i-1] = va_arg(ap, SetWordType *);
  136.     }
  137.     for (i=1; i<=k; i++)    /* look for offending token */
  138.     {
  139.         if ( i>1 ) strcat(text, " ");
  140.         strcat(text, LATEXT(i));
  141.         if ( !zzset_el((unsigned)LA(i), f[i-1]) ) break;
  142.     }
  143.     miss_set = va_arg(ap, SetWordType **);
  144.     miss_text = va_arg(ap, char **);
  145.     bad_tok = va_arg(ap, int *);
  146.     bad_text = va_arg(ap, char **);
  147.     err_k = va_arg(ap, int *);
  148.     if ( i>k )
  149.     {
  150.         /* bad; lookahead is permutation that cannot be matched,
  151.          * but, the ith token of lookahead is valid at the ith position
  152.          * (The old LL sub 1 (k) versus LL(k) parsing technique)
  153.          */
  154.         *miss_set = NULL;
  155.         *miss_text = zzlextext;
  156.         *bad_tok = LA(1);
  157.         *bad_text = LATEXT(1);
  158.         *err_k = k;
  159.         return;
  160.     }
  161. /*    fprintf(stderr, "%s not in %dth set\n", zztokens[LA(i)], i);*/
  162.     *miss_set = f[i-1];
  163.     *miss_text = text;
  164.     *bad_tok = LA(i);
  165.     *bad_text = LATEXT(i);
  166.     if ( i==1 ) *err_k = 1;
  167.     else *err_k = k;
  168. }
  169.  
  170. void
  171. #ifdef __USE_PROTOS
  172. zzsave_antlr_state(zzantlr_state *buf)
  173. #else
  174. zzsave_antlr_state(buf)
  175. zzantlr_state *buf;
  176. #endif
  177. {
  178. #ifdef LL_K
  179.     int i;
  180. #endif
  181.  
  182. #ifdef ZZCAN_GUESS
  183.     buf->guess_start = zzguess_start;
  184.     buf->guessing = zzguessing;
  185. #endif
  186.     buf->asp = zzasp;
  187. #ifdef GENAST
  188.     buf->ast_sp = zzast_sp;
  189. #endif
  190. #ifdef ZZINF_LOOK
  191.     buf->inf_labase = zzinf_labase;
  192.     buf->inf_last = zzinf_last;
  193. #endif
  194. #ifdef DEMAND_LOOK
  195.     buf->dirty = zzdirty;
  196. #endif
  197. #ifdef LL_K
  198.     for (i=0; i<LL_K; i++) buf->tokenLA[i] = zztokenLA[i];
  199.     for (i=0; i<LL_K; i++) strcpy(buf->textLA[i], zztextLA[i]);
  200.     buf->lap = zzlap;
  201.     buf->labase = zzlabase;
  202. #else
  203.     buf->token = zztoken;
  204.     strcpy(buf->text, zzlextext);
  205. #endif
  206. }
  207.  
  208. void
  209. #ifdef __USE_PROTOS
  210. zzrestore_antlr_state(zzantlr_state *buf)
  211. #else
  212. zzrestore_antlr_state(buf)
  213. zzantlr_state *buf;
  214. #endif
  215. {
  216. #ifdef LL_K
  217.     int i;
  218. #endif
  219.  
  220. #ifdef ZZCAN_GUESS
  221.     zzguess_start = buf->guess_start;
  222.     zzguessing = buf->guessing;
  223. #endif
  224.     zzasp = buf->asp;
  225. #ifdef GENAST
  226.     zzast_sp = buf->ast_sp;
  227. #endif
  228. #ifdef ZZINF_LOOK
  229.     zzinf_labase = buf->inf_labase;
  230.     zzinf_last = buf->inf_last;
  231. #endif
  232. #ifdef DEMAND_LOOK
  233.     zzdirty = buf->dirty;
  234. #endif
  235. #ifdef LL_K
  236.     for (i=0; i<LL_K; i++) zztokenLA[i] = buf->tokenLA[i];
  237.     for (i=0; i<LL_K; i++) strcpy(zztextLA[i], buf->textLA[i]);
  238.     zzlap = buf->lap;
  239.     zzlabase = buf->labase;
  240. #else
  241.     zztoken = buf->token;
  242.     strcpy(zzlextext, buf->text);
  243. #endif
  244. }
  245.  
  246. void
  247. #ifdef __USE_PROTOS
  248. zzedecode(SetWordType *a)
  249. #else
  250. zzedecode(a)
  251. SetWordType *a;
  252. #endif
  253. {
  254.     register SetWordType *p = a;
  255.     register SetWordType *endp = &(p[zzSET_SIZE]);
  256.     register unsigned e = 0;
  257.  
  258.     if ( zzset_deg(a)>1 ) fprintf(stderr, " {");
  259.     do {
  260.         register SetWordType t = *p;
  261.         register SetWordType *b = &(bitmask[0]);
  262.         do {
  263.             if ( t & *b ) fprintf(stderr, " %s", zztokens[e]);
  264.             e++;
  265.         } while (++b < &(bitmask[sizeof(SetWordType)*8]));
  266.     } while (++p < endp);
  267.     if ( zzset_deg(a)>1 ) fprintf(stderr, " }");
  268. }
  269.  
  270. #ifndef USER_ZZSYN
  271. /* standard error reporting function */
  272. void
  273. #ifdef __USE_PROTOS
  274. zzsyn(char *text, int tok, char *egroup, SetWordType *eset, int etok, int k, char *bad_text)
  275. #else
  276. zzsyn(text, tok, egroup, eset, etok, k, bad_text)
  277. char *text, *egroup, *bad_text;
  278. int tok;
  279. int etok;
  280. int k;
  281. SetWordType *eset;
  282. #endif
  283. {
  284.     
  285.     fprintf(stderr, "line %d: syntax error at \"%s\"", zzline, (tok==zzEOF_TOKEN)?"EOF":bad_text);
  286.     if ( !etok && !eset ) {fprintf(stderr, "\n"); return;}
  287.     if ( k==1 ) fprintf(stderr, " missing");
  288.     else
  289.     {
  290.         fprintf(stderr, "; \"%s\" not", bad_text);
  291.         if ( zzset_deg(eset)>1 ) fprintf(stderr, " in");
  292.     }
  293.     if ( zzset_deg(eset)>0 ) zzedecode(eset);
  294.     else fprintf(stderr, " %s", zztokens[etok]);
  295.     if ( strlen(egroup) > 0 ) fprintf(stderr, " in %s", egroup);
  296.     fprintf(stderr, "\n");
  297. }
  298. #endif
  299.  
  300. /* is b an element of set p? */
  301. int
  302. #ifdef __USE_PROTOS
  303. zzset_el(unsigned b, SetWordType *p)
  304. #else
  305. zzset_el(b,p)
  306. unsigned b;
  307. SetWordType *p;
  308. #endif
  309. {
  310.     return( p[BSETDIVWORD(b)] & bitmask[BSETMODWORD(b)] );
  311. }
  312.  
  313. int
  314. #ifdef __USE_PROTOS
  315. zzset_deg(SetWordType *a)
  316. #else
  317. zzset_deg(a)
  318. SetWordType *a;
  319. #endif
  320. {
  321.     /* Fast compute degree of a set... the number
  322.        of elements present in the set.  Assumes
  323.        that all word bits are used in the set
  324.     */
  325.     register SetWordType *p = a;
  326.     register SetWordType *endp = &(a[zzSET_SIZE]);
  327.     register int degree = 0;
  328.  
  329.     if ( a == NULL ) return 0;
  330.     while ( p < endp )
  331.     {
  332.         register SetWordType t = *p;
  333.         register SetWordType *b = &(bitmask[0]);
  334.         do {
  335.             if (t & *b) ++degree;
  336.         } while (++b < &(bitmask[sizeof(SetWordType)*8]));
  337.         p++;
  338.     }
  339.  
  340.     return(degree);
  341. }
  342.  
  343. #ifdef DEMAND_LOOK
  344.  
  345. #ifdef LL_K
  346. int
  347. #ifdef __USE_PROTOS
  348. _zzmatch(int _t, char **zzBadText, char **zzMissText,
  349.         int *zzMissTok, int *zzBadTok,
  350.         SetWordType **zzMissSet)
  351. #else
  352. _zzmatch(_t, zzBadText, zzMissText, zzMissTok, zzBadTok, zzMissSet)
  353. int _t;
  354. char **zzBadText;
  355. char **zzMissText;
  356. int *zzMissTok, *zzBadTok;
  357. SetWordType **zzMissSet;
  358. #endif
  359. {
  360.     if ( zzdirty==LL_K ) {
  361.         zzCONSUME;
  362.     }
  363.     if ( LA(1)!=_t ) {
  364.         *zzBadText = *zzMissText=LATEXT(1);    
  365.         *zzMissTok= _t; *zzBadTok=LA(1); 
  366.         *zzMissSet=NULL;                
  367.         return 0;
  368.     }
  369.     zzMakeAttr                        
  370.     zzdirty++;                        
  371.     zzlabase++;                        
  372.     return 1;
  373. }
  374.  
  375. #else
  376.  
  377. int
  378. #ifdef __USE_PROTOS
  379. _zzmatch(int _t, char **zzBadText, char **zzMissText,
  380.          int *zzMissTok, int *zzBadTok, SetWordType **zzMissSet)
  381. #else
  382. _zzmatch(_t, zzBadText, zzMissText, zzMissTok, zzBadTok, zzMissSet)
  383. int _t;
  384. char **zzBadText;
  385. char **zzMissText;
  386. int *zzMissTok, *zzBadTok;
  387. SetWordType **zzMissSet;
  388. #endif
  389. {                                
  390.     if ( zzdirty ) {zzCONSUME;}        
  391.     if ( LA(1)!=_t ) {
  392.         *zzBadText = *zzMissText=LATEXT(1);    
  393.         *zzMissTok= _t; *zzBadTok=LA(1); 
  394.         *zzMissSet=NULL;                
  395.         return 0;
  396.     }                                
  397.     zzdirty = 1;                    
  398.     zzMakeAttr                        
  399.     return 1;
  400. }
  401. #endif /*LL_K*/
  402.  
  403. #else
  404.  
  405. int
  406. #ifdef __USE_PROTOS
  407. _zzmatch(int _t, char **zzBadText, char **zzMissText,
  408.         int *zzMissTok, int *zzBadTok,
  409.         SetWordType **zzMissSet)
  410. #else
  411. _zzmatch(_t, zzBadText, zzMissText, zzMissTok, zzBadTok, zzMissSet)
  412. int _t;
  413. char **zzBadText;
  414. char **zzMissText;
  415. int *zzMissTok, *zzBadTok;
  416. SetWordType **zzMissSet;
  417. #endif
  418. {
  419.     if ( LA(1)!=_t ) {                
  420.         *zzBadText = *zzMissText=LATEXT(1);    
  421.         *zzMissTok= _t; *zzBadTok=LA(1); 
  422.         *zzMissSet=NULL;                
  423.         return 0;
  424.     }
  425.     zzMakeAttr
  426.     return 1;
  427. }
  428. #endif /*DEMAND_LOOK*/
  429.  
  430. #ifdef ZZINF_LOOK
  431. void
  432. #ifdef __USE_PROTOS
  433. _inf_zzgettok(void)
  434. #else
  435. _inf_zzgettok()
  436. #endif
  437. {
  438.     if ( zzinf_labase >= zzinf_last )                    
  439.         {NLA = zzEOF_TOKEN; strcpy(NLATEXT, "");}    
  440.     else {                                            
  441.         NLA = zzinf_tokens[zzinf_labase];
  442.         zzline = zzinf_line[zzinf_labase];    /* wrong in 1.21 */
  443.         strcpy(NLATEXT, zzinf_text[zzinf_labase]);        
  444.         zzinf_labase++;                                 
  445.     }                                                
  446. }
  447. #endif
  448.  
  449. #ifdef ZZINF_LOOK
  450. /* allocate default size text,token and line arrays;
  451.  * then, read all of the input reallocing the arrays as needed.
  452.  * Once the number of total tokens is known, the LATEXT(i) array (zzinf_text)
  453.  * is allocated and it's pointers are set to the tokens in zzinf_text_buffer.
  454.  */
  455. void
  456. #ifdef __USE_PROTOS
  457. zzfill_inf_look(void)
  458. #else
  459. zzfill_inf_look()
  460. #endif
  461. {
  462.     int tok, line;
  463.     int zzinf_token_buffer_size = ZZINF_DEF_TOKEN_BUFFER_SIZE;
  464.     int zzinf_text_buffer_size = ZZINF_DEF_TEXT_BUFFER_SIZE;
  465.     int zzinf_text_buffer_index = 0;
  466.     int zzinf_lap = 0;
  467.  
  468.     /* allocate text/token buffers */
  469.     zzinf_text_buffer = (char *) malloc(zzinf_text_buffer_size);
  470.     if ( zzinf_text_buffer == NULL )
  471.     {
  472.         fprintf(stderr, "cannot allocate lookahead text buffer (%d bytes)\n", 
  473.         zzinf_text_buffer_size);
  474.         exit(-1);                                        
  475.     }
  476.     zzinf_tokens = (int *) calloc(zzinf_token_buffer_size,sizeof(int)); 
  477.     if ( zzinf_tokens == NULL )
  478.     {
  479.         fprintf(stderr,    "cannot allocate token buffer (%d tokens)\n", 
  480.                 zzinf_token_buffer_size);
  481.         exit(-1);                                        
  482.     }
  483.     zzinf_line = (int *) calloc(zzinf_token_buffer_size,sizeof(int));
  484.     if ( zzinf_line == NULL )
  485.     {
  486.         fprintf(stderr, "cannot allocate line buffer (%d ints)\n",
  487.                 zzinf_token_buffer_size);
  488.         exit(-1);
  489.     }
  490.  
  491.     /* get tokens, copying text to text buffer */
  492.     zzinf_text_buffer_index = 0;
  493.     do {
  494.         zzgettok();
  495.         line = zzreal_line;
  496.         while ( zzinf_lap>=zzinf_token_buffer_size )
  497.         {
  498.             zzinf_token_buffer_size += ZZINF_BUFFER_TOKEN_CHUNK_SIZE; 
  499.             zzinf_tokens = (int *) realloc(zzinf_tokens,
  500.                                                  zzinf_token_buffer_size*sizeof(int));
  501.             if ( zzinf_tokens == NULL )
  502.             {
  503.                 fprintf(stderr, "cannot allocate lookahead token buffer (%d tokens)\n", 
  504.                         zzinf_token_buffer_size);
  505.                 exit(-1);
  506.             }
  507.             zzinf_line = (int *) realloc(zzinf_line,
  508.                                          zzinf_token_buffer_size*sizeof(int));
  509.             if ( zzinf_line == NULL )
  510.             {
  511.                 fprintf(stderr, "cannot allocate lookahead line buffer (%d ints)\n",
  512.                         zzinf_token_buffer_size);
  513.                 exit(-1);
  514.             }
  515.  
  516.         }
  517.         while ( (zzinf_text_buffer_index+strlen(NLATEXT)+1) >= zzinf_text_buffer_size )
  518.         {
  519.             zzinf_text_buffer_size += ZZINF_BUFFER_TEXT_CHUNK_SIZE; 
  520.             zzinf_text_buffer = (char *) realloc(zzinf_text_buffer,
  521.                                                  zzinf_text_buffer_size);
  522.             if ( zzinf_text_buffer == NULL )
  523.             {
  524.                 fprintf(stderr,    "cannot allocate lookahead text buffer (%d bytes)\n", 
  525.                         zzinf_text_buffer_size);
  526.                 exit(-1);
  527.             }
  528.         }
  529.         /* record token and text and line of input symbol */
  530.         tok = zzinf_tokens[zzinf_lap] = NLA;
  531.         strcpy(&zzinf_text_buffer[zzinf_text_buffer_index], NLATEXT);
  532.         zzinf_text_buffer_index += strlen(NLATEXT)+1;
  533.         zzinf_line[zzinf_lap] = line;
  534.         zzinf_lap++;
  535.     } while (tok!=zzEOF_TOKEN);
  536.     zzinf_labase = 0;
  537.     zzinf_last = zzinf_lap-1;
  538.  
  539.     /* allocate ptrs to text of ith token */
  540.     zzinf_text = (char **) calloc(zzinf_last+1,sizeof(char *));
  541.     if ( zzinf_text == NULL )
  542.     {
  543.         fprintf(stderr,    "cannot allocate lookahead text buffer (%d)\n", 
  544.                 zzinf_text_buffer_size); 
  545.         exit(-1);                                            
  546.     }                                                        
  547.     zzinf_text_buffer_index = 0;
  548.     zzinf_lap = 0;
  549.     /* set ptrs so that zzinf_text[i] is the text of the ith token found on input */
  550.     while (zzinf_lap<=zzinf_last)
  551.     {
  552.         zzinf_text[zzinf_lap++] = &zzinf_text_buffer[zzinf_text_buffer_index]; 
  553.         zzinf_text_buffer_index += strlen(&zzinf_text_buffer[zzinf_text_buffer_index])+1; 
  554.     }
  555. }
  556. #endif
  557.  
  558. int
  559. #ifdef __USE_PROTOS
  560. _zzsetmatch(SetWordType *e, char **zzBadText, char **zzMissText,
  561.             int *zzMissTok, int *zzBadTok,
  562.             SetWordType **zzMissSet)
  563. #else
  564. _zzsetmatch(e, zzBadText, zzMissText, zzMissTok, zzBadTok, zzMissSet)
  565. SetWordType *e;
  566. char **zzBadText;
  567. char **zzMissText;
  568. int *zzMissTok, *zzBadTok;
  569. SetWordType **zzMissSet;
  570. #endif
  571. {
  572. #ifdef DEMAND_LOOK
  573. #ifdef LL_K
  574.     if ( zzdirty==LL_K ) {zzCONSUME;}
  575. #else
  576.     if ( zzdirty ) {zzCONSUME;}
  577. #endif
  578. #endif
  579.     if ( !zzset_el((unsigned)LA(1), e) ) {
  580.         *zzBadText = LATEXT(1); *zzMissText=NULL;
  581.         *zzMissTok= 0; *zzBadTok=LA(1);
  582.         *zzMissSet=e;
  583.         return 0;
  584.     }
  585. #ifdef DEMAND_LOOK
  586. #ifdef LL_K
  587.     zzdirty++;
  588. #else
  589.     zzdirty = 1;
  590. #endif
  591. #endif
  592.     zzMakeAttr
  593.     return 1;
  594. }
  595. #endif /* ERR_H */
  596.